package com.shop.util;

import com.shop.bean.User;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpSession;

public class SYS {
    public static final String FILE_UPLOAD_DIR="D:\\situwork\\Shoe Dream of Riding the Tide\\";

    public static final String FILE_UPLOAD_URL="shopfile";

    //后台管理系统上传类别的图片                                    
    public static final String IMG_UPLOAD_DIR_SYSTEM_CATEGORY="D:\\situwork\\Shoe Dream of Riding the Tide\\img\\system\\category\\";
    public static final String IMG_UPLOAD_URL_SYSTEM_CATEGORY="shopfile/img/system/category";

    //后台管理系统上传商品的图片
    public static final String IMG_UPLOAD_DIR_SYSTEM_PRODUCT="D:\\situwork\\Shoe Dream of Riding the Tide\\img\\system\\product\\";
    public static final String IMG_UPLOAD_URL_SYSTEM_PRODUCT="shopfile/img/system/product";

    //用户上传头像
    public static final String IMG_UPLOAD_DIR_SHOP_USER="D:\\situwork\\Shoe Dream of Riding the Tide\\img\\shop\\user\\";
    public static final String IMG_UPLOAD_URL_SHOP_USER="shopfile/img/shop/user";


    //用户登录用的参数
    public static final String SHOPPING_LOGIN_USER="SHOPPING_LOGIN_USER";

    public static final String SYSTEM_LOGIN_ADMIN="SYSTEM_LOGIN_ADMIN";




    //最大收货地址数量
    public static final int ADDREE_MAX_NUMBER=6;

    //16进制
    public static final int[] HEX_CHAR_ARR={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};


    //返回个用户ID
    public static int getLoginUserID(HttpSession session){
        User user=(User)session.getAttribute(SHOPPING_LOGIN_USER);
        return user.getId();
    }

    //加密
    public static String digest(String pass) {
        //随机次数随机盐值加密
        int countIndex=(int)(Math.random()*HEX_CHAR_ARR.length);
        int saltIndex=(int)(Math.random()*HEX_CHAR_ARR.length);
        int count=HEX_CHAR_ARR[countIndex];
        char salt=(char)HEX_CHAR_ARR[saltIndex];
        char countChar=(char)count;
        pass=pass+salt;

        for(int i=0;i<count;i++){
            pass= DigestUtils.md5DigestAsHex(pass.getBytes());
        }
        String passwordText=salt+pass+countChar;
        return passwordText;
    }

    //登录验证方法
    public static boolean checkPass(String pass,String hexPass){
        char salt=hexPass.charAt(0);//盐值
        char countChar=hexPass.charAt(hexPass.length()-1);//次数
        pass=pass+salt;
        for(int i=0;i<countChar;i++){
            pass=DigestUtils.md5DigestAsHex(pass.getBytes());
        }
        String text=salt+pass+countChar;
        return text.equals(hexPass);
    }
}